module executor.message.server.TransferMsgExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import hunt.util.Serialize;
import client.MsgServerClient;
import client.ImClient;
import client.LogicServerClient;
import std.array;
import std.conv : to;
import core.stdc.time;

@TransportServer(gateServerName)
class TransferMsgExecutor : AbstractExecutor!(TransferMsgExecutor) {

    @MessageId(MESSAGE.CMD_MESSAGE_TRANSFER)
    void sendMsg(TransportContext ctx, MessageBuffer buffer) {
        auto req = new Msg();
        buffer.data.fromProtobuf!Msg(req);
        req.msgTime = core.stdc.time.time(null);
        logInfo("ws-gate server forward msg to msg server : ",toJson(req));
        if (req.chatId.length == 0)
        {
            logError("Unable to match MsgServer, parameter chatId is null.");
            return;
        }

        MessageTransportClient client = MsgServerClient.getInstance.getForwardClient(req.chatId);
        client.send(new MessageBuffer(MESSAGE.CMD_MESSAGE_TRANSFER,req.toProtobuf.array));
    }

    @MessageId(MESSAGE.CMD_GET_HISTORY_REQ)
    void getHistoryMsg(TransportContext ctx, MessageBuffer buffer) {
        auto req = new HistoryMsgREQ();
        buffer.data.fromProtobuf!HistoryMsgREQ(req);

        User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
        AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
        if(u !is null)
        {
            req.src = authorize.src;
        }

        MessageTransportClient client = LogicServerClient.getInstance().getForwardClient();
        client.send(new MessageBuffer(MESSAGE.CMD_GET_HISTORY_REQ,req.toProtobuf.array));
    }

    @MessageId(MESSAGE.CMD_GET_SESSION_LIST)
    void getSessionList(TransportContext ctx, MessageBuffer buffer) {
        auto req = new SessionList();
        buffer.data.fromProtobuf!SessionList(req);

        User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
        AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
        if(u !is null)
        {
            req.src = authorize.src;
        }

        MessageTransportClient client = LogicServerClient.getInstance().getForwardClient();
        client.send(new MessageBuffer(MESSAGE.CMD_GET_SESSION_LIST,req.toProtobuf.array));
    }


    @MessageId(MESSAGE.CMD_SET_MSG_READ_REQ)
    void getMsgRead(TransportContext ctx, MessageBuffer buffer) {
        auto req = new AlreadyReadREQ();
        buffer.data.fromProtobuf!AlreadyReadREQ(req);
        req.timestamp = core.stdc.time.time(null);

        User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);

        if(u !is null)
        {
                req.clientId = u.id;
        }
        else
        {
                logError("client is not login");
                return;
        }

        MessageTransportClient client = MsgServerClient.getInstance.getForwardClient(req.chatId);
        client.send(new MessageBuffer(MESSAGE.CMD_SET_MSG_READ_REQ,req.toProtobuf.array));
    }
}

